<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * PHP version 5
 *
 * This source file is subject to version 2.1 of the GNU Lesser General Public
 * License, that is bundled with this package in the file COPYING, available
 * through the world wide web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html.
 *
 *Razred ki uporavlja z vlogami uporabnikov. Vsebuje tudi metode za administracijo.
 *
 * @package    User
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @link       http://www.zeronotice.org
 */

require_once('ZNF/Business/MDB2.php');
require_once('ZNF/Util/Utility.php');

class User_Business_RoleDAO
{

    private $_db = null;
    private $_tablePrefix = null;


    public function __construct()
    {
        $this->_db = ZNF_Business_MDB2::getInstance();
        $this->_tablePrefix = ZNF_Business_MDB2::getTablePrefix();
        $this->_db->setCharset('utf8');
    }

    /**
     *Metoda za vnasanje novih vlog v bazo.
     *
     *@param string $name Ime vloge. Slednje mora biti razlicno od vseh ostalih (unique)!
     *@param string $description Kratek opis vloge.
     **/


    public function insert($name, $description)
    {
        $sql = 'INSERT INTO ' . $this->_tablePrefix . "UserRole(name, description)
                VALUES ('$name', '$description')";
        $res = $this->_db->exec($sql);
        if (PEAR::isError($res)) {
            throw new User_Business_User_Business_RoleDAOException('errorRoleInsert - '. $res->getMessage());
        }
    }

    /**
     *Metoda spremeni ime in opis vloge v bazi. Nov opis je lahko null, v tem primeru bo metoda spremenila le ime vloge.
     *@param string $name Staro ime vloge.
     *@param string $newName Novo ime vloge.
     *@param string $newDescription Nov opis vloge.
     **/

    public function update($name, $newName, $newDescription = null)
    {
        $update = 'UPDATE ' . $this->_tablePrefix . "UserRole SET
                    name='$newName'";
        $description = ", description='$newDescription'";
        $whereClause = " WHERE name='$name'";
        $res = $this->_db->exec($sql . (newDescription == null ? $whereClause : $description.$whereClause));
        if (PEAR::isError($res)) {
            throw new User_Business_User_Business_RoleDAOException('errorRoleUpdate - '. $res->getMessage());
        }
        return true;
    }


    /**
     *Metoda zbrise vlogo iz baze.
     *@param string $name Vloga, ki jo zelimo zbrisati.
     **/
    
    public function delete($roleName)
    {
        $sql = 'DELETE FROM ' . $this->_tablePrefix . "UserRole WHERE name='$roleName'";
        $res = $this->_db->exec($sql);
        if (PEAR::isError($res)) {
            throw new User_Business_User_Business_RoleDAOException('errorRoleDelete - '. $res->getMessage());
        }
        return true;
    }
    
    /**
     *Metoda asocira uporabniku vlogo.
     *@param string $roleName Ime vloge, ki jo zelimo dodeliti uporabniku.
     *@param integer $userId ID uporabnika, ki mu zelimo asocirati vlogo.
     **/

    public function associateUserRole($roleName, $userId = null)
    {
        if($userId == null){
            $userId = $this->_db->lastInsertID('User');
            if(PEAR::isError($userId)){
                throw new User_Business_User_Business_RoleDAOException('errorUserRoleAssociation - '. $res->getMessage());
            }
        }
        $sql = 'INSERT INTO ' . $this->_tablePrefix . "UserRoleAssociation(userId, userRoleName)
                VALUES ('$userId', '$roleName')";
        $res = $this->_db->exec($sql);
        if (PEAR::isError($res)) {
            throw new User_Business_User_Business_RoleDAOException('errorUserRoleAssociation - '. $res->getMessage());
        }
    }

    /**
     *Metoda odvzame vlogo danemu uporabniku.
     *@param integer $userId ID uporabnika, ki mo zelimo odvzeti dano vlogo.
     *@param string $roleName Ime vloge, ki jo zelimo odvzeti uporabniku.
     **/

    public function unassociateUserRole($userId, $roleName)
    {
        $sql = 'DELETE FROM ' . $this->_tablePrefix . "UserRoleAssociation
                WHERE userId='$userId' AND userRoleName='$roleName'";
        $res = $this->_db->exec($sql);
        if (PEAR::isError($res)) {
            throw new User_Business_User_Business_RoleDAOException('erroruserRoleUnassociation - '. $res->getMessage());
        }
    }
    
    /**
     *Metoda poisce vse vloge, asocirane danemu uporabniku.
     *@param integer $userId Uporabnik, katerega vloge zelimo poiskati vloge.
     *@return array Vloge, asocirane uporabniku.
     **/

    public function getUserRoles($userId)
    {
        $sql = 'SELECT * FROM ' . $this->_tablePrefix . "UserRoleAssociation WHERE userId='$userId'";
        $rows = $this->_db->queryAll($sql);
        if (PEAR::isError($rows)) {
            throw new User_Business_User_Business_RoleDAOException('errorRoleLoad - '. $rows->getMessage());
        }
        $rows = ZNF_Util_Utility::stripAllSlashes($rows);
        return $rows;
    }

    public function __destruct()
    {
    }

}

class User_Business_User_Business_RoleDAOException extends Exception
{
}

?>
